<template>
	<view class="leave_box">
    <navTitle :title="'申請記錄'"></navTitle>
    <view class="row-p margin-t-10" v-for="(item,index) in recordList" :key="index">
      <view @tap="handleDetail(item,index)">
        <view class="p-title">
          <view class="p-t-l">{{userInfo.nickname}}提交的請假<text>{{ item.createtimeText }}</text></view>
          <view class="p-t-r"><text :class="item.status==0?'blue':item.status==1?'green':'red'">{{ item.status==0?'正在審批':item.status==1?'審批通過':'審批未通過' }}</text></view>
        </view>
        <view class="p-row margin-b-10 margin-t-10">請假類型：{{ item.type_txt }}</view>
        <view class="p-row margin-b-10">開始時間：{{ item.begin_time_text }}</view>
        <view class="p-row margin-b-10">結束時間：{{ item.end_time_text }}</view>
      </view>
      <view class="p-btn">
        <block v-if="item.status==0">
          <view @tap="handleCX(item,index)">撤銷</view>
          <view @tap="handleEdit(item,index)">修改</view>
        </block>
        <block v-if="item.status==2">
          <view @tap="handleEdit(item,index)" style="margin-right: 0;">修改</view>
        </block>
      </view>
    </view>
	</view>
</template>

<script>
import { askleaveLists,askleaveCancel } from '../../utils/api'
import navTitle from './components/nav-title.vue';
export default {
	components: {navTitle},
	data() {
		return {
      checkedOn:true,
      checkedOut:false,
      params:{
        begin:'',
        end:'',
        offset:1,
        limit:10,
      },
      recordList:[],
      userInfo:{},
      has_more:true,
		};
	},
	props: {
	},
	computed: {},
  onShow() {
    this.userInfo=uni.getStorageSync('userInfo')
    this.getRecord()
  },
  onPullDownRefresh() {
    this.params.offset=1
    this.recordList=[]
    this.getRecord()
  },
  onReachBottom() {
    if(this.has_more){
      this.params.offset+=1
      this.getRecord()
    }else{
      uni.showToast({
        icon:'none',
        title: '到底啦～'
      })
    }
  },
	methods: {
    formatTimestamp(timestamp) {
      const momentObj = this.$moment(timestamp*1000)
      return momentObj.format('YYYY-MM-DD')
      //  HH:mm:ss
    },
    async getRecord(){
      let res=await askleaveLists(this.params)
      if(res.code==1){
        this.has_more=res.data.has_more
        let list=res.data.data.map(v=>{
          return {...v,createtimeText:this.formatTimestamp(v.createtime)}
        })
        this.recordList=this.recordList.concat(list)
      }else{
        uni.showToast({
          icon:'none',
          title: res.msg||'系統異常'
        })
      }
    },
    handleDetail(item,index){
      uni.navigateTo({ url: `/pages/boss/leaveDetail?id=${item.id}`})
    },
    handleEdit(item,index){
      uni.navigateTo({ url: `/pages/boss/leaveEdit?id=${item.id}`})
    },
    async formCancel(id){
      let that=this
      let res=await askleaveCancel({id:id})
      if(res.code==1){
        this.getRecord()
      }else{
        uni.showToast({
          icon:'none',
          title: res.msg||'系統異常'
        })
      }
    },
    handleCX(item,index){
      let that=this
      uni.showModal({
        title: '提示',
        content: '确定要撤销该申请吗？',
        showCancel: true,
        success: ({ confirm, cancel }) => {
          that.formCancel(item.id)
        }
      })
    },
	}
};
</script>
<style lang="scss" scoped>
.leave_box{
  width: 100%;
  height: 100vh;
  overflow-y: scroll;
  background: #F4F6FA;
}
.row-p{
  width: calc(100% - 40px);
  padding: 15px 10px;
  margin: 0 auto 10px;
  background: #fff;
  border-radius: 10px;
}
.p-title{
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .p-t-l{
    flex: 3;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-weight: bold;
    font-size: 16px;
    color: #333333;
    line-height: 23px;
    text{
      font-size: 13px;
      color: #999999;
      margin-left: 4px;
    }
  }
  .p-t-r{
    flex: 1;
    font-weight: bold;
    font-size: 15px;
    line-height: 14px;
    text-align: right;
    text.blue{
      color: #1C7AF7;
    }
    text.green{
      color: #1EB50F;
    }
    text.red{
      color: #DD2727;
    }
  }
}
.p-row{
  font-size: 14px;
  color: #333333;
  line-height: 14px;
}
.margin-b-10{
  margin-bottom: 10px;
}
.margin-t-10{
  margin-top: 10px;
}
.p-btn{
  display: flex;
  justify-content: flex-end;
  align-items: center;
  view{
    width: 32px;
    height: 16px;
    font-size: 16px;
    color: #1C7AF7;
    line-height: 16px;
  }
  view:first-child{
    margin-right: 20px;
  }
}
</style>
